package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.BusQingjiaListBo;
import com.ruoyi.system.domain.vo.BusQingjiaListVo;
import com.ruoyi.system.domain.BusQingjiaList;
import com.ruoyi.system.mapper.BusQingjiaListMapper;
import com.ruoyi.system.service.IBusQingjiaListService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 请假单明细Service业务层处理
 *
 * @author yegs
 * @date 2022-11-30
 */
@RequiredArgsConstructor
@Service
public class BusQingjiaListServiceImpl implements IBusQingjiaListService {

    private final BusQingjiaListMapper baseMapper;

    /**
     * 查询请假单明细
     */
    @Override
    public BusQingjiaListVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询请假单明细列表
     */
    @Override
    public TableDataInfo<BusQingjiaListVo> queryPageList(BusQingjiaListBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BusQingjiaList> lqw = buildQueryWrapper(bo);
        Page<BusQingjiaListVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询请假单明细列表
     */
    @Override
    public List<BusQingjiaListVo> queryList(BusQingjiaListBo bo) {
        LambdaQueryWrapper<BusQingjiaList> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BusQingjiaList> buildQueryWrapper(BusQingjiaListBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BusQingjiaList> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getBillno()), BusQingjiaList::getBillno, bo.getBillno());
        lqw.eq(bo.getLineno() != null, BusQingjiaList::getLineno, bo.getLineno());
        lqw.eq(StringUtils.isNotBlank(bo.getUserid()), BusQingjiaList::getUserid, bo.getUserid());
        lqw.like(StringUtils.isNotBlank(bo.getUsername()), BusQingjiaList::getUsername, bo.getUsername());
        lqw.eq(StringUtils.isNotBlank(bo.getUsercode()), BusQingjiaList::getUsercode, bo.getUsercode());
        lqw.eq(StringUtils.isNotBlank(bo.getGonghao()), BusQingjiaList::getGonghao, bo.getGonghao());
        lqw.eq(StringUtils.isNotBlank(bo.getDeptid()), BusQingjiaList::getDeptid, bo.getDeptid());
        lqw.like(StringUtils.isNotBlank(bo.getDeptname()), BusQingjiaList::getDeptname, bo.getDeptname());
        lqw.eq(StringUtils.isNotBlank(bo.getBanzu()), BusQingjiaList::getBanzu, bo.getBanzu());
        lqw.eq(bo.getSdate() != null, BusQingjiaList::getSdate, bo.getSdate());
        lqw.eq(StringUtils.isNotBlank(bo.getEdate()), BusQingjiaList::getEdate, bo.getEdate());
        lqw.eq(StringUtils.isNotBlank(bo.getCdate()), BusQingjiaList::getCdate, bo.getCdate());
        lqw.eq(bo.getVoudate() != null, BusQingjiaList::getVoudate, bo.getVoudate());
        lqw.eq(StringUtils.isNotBlank(bo.getMarker()), BusQingjiaList::getMarker, bo.getMarker());
        lqw.eq(StringUtils.isNotBlank(bo.getExt01()), BusQingjiaList::getExt01, bo.getExt01());
        lqw.eq(StringUtils.isNotBlank(bo.getExt02()), BusQingjiaList::getExt02, bo.getExt02());
        lqw.eq(StringUtils.isNotBlank(bo.getExt03()), BusQingjiaList::getExt03, bo.getExt03());
        lqw.eq(StringUtils.isNotBlank(bo.getExt04()), BusQingjiaList::getExt04, bo.getExt04());
        lqw.eq(StringUtils.isNotBlank(bo.getExt05()), BusQingjiaList::getExt05, bo.getExt05());
        return lqw;
    }

    /**
     * 新增请假单明细
     */
    @Override
    public Boolean insertByBo(BusQingjiaListBo bo) {
        BusQingjiaList add = BeanUtil.toBean(bo, BusQingjiaList.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改请假单明细
     */
    @Override
    public Boolean updateByBo(BusQingjiaListBo bo) {
        BusQingjiaList update = BeanUtil.toBean(bo, BusQingjiaList.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BusQingjiaList entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除请假单明细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
